//
//  UserPickerController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/27.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserPickerController.h"
#import "FlatUIKit.h"
#import "UserGroup.h"
#import "UserItem.h"

@interface UserPickerController ()<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
/** textField */
@property (nonatomic, strong) FUITextField *textField;

@property (nonatomic, strong) UIPickerView *pickerView;


@end

@implementation UserPickerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.contentKey;
    self.view.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    
    [self setTextFieldUpUI];
    [self setPickerViewUpUI];
    
    
    [self.pickerDataSource enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([self.contenValue isEqualToString:obj]) {
            [self.pickerView selectRow: idx inComponent:0 animated:NO];
            *stop = YES;
        }
    }];
    
    [self configureNav];
    
}

- (void)configureNav{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sport_icon_blackbackArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.textField becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (self.selectCallback) {
        self.selectCallback(self.textField.text);
    }
}

- (void)setTextFieldUpUI {
    
    CGFloat textFieldHeight = kAutoHei(44);
    NSString *leftTextStr = self.contentKey;

    if ([NSBundle currentLangeuage] == kLanguageFrennch && kYFScreenWidth <= 375) {
        textFieldHeight = kAutoHei(64);
        leftTextStr = @"Nbre d'années d'\nexpérience tenn";
    }
    
    FUITextField *textField = [[FUITextField alloc] initWithFrame:CGRectMake(0, kAutoHei(24), kYFScreenWidth, textFieldHeight)];
    
    self.textField = textField;
    self.textField.tintColor = [UIColor clearColor];
    textField.textColor = [UIColor colorWithHex:0x787878];
    textField.font = [UIFont systemFontOfSize:16];
    
    textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textField];
    textField.delegate = self;
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.text = leftTextStr;
    leftLabel.textAlignment = NSTextAlignmentCenter;
    leftLabel.textColor = [UIColor colorWithHex:0x151515];
    leftLabel.font = [UIFont systemFontOfSize:18];
    leftLabel.numberOfLines = 0;
    
    [leftLabel sizeToFit];
    CGFloat labelW = leftLabel.bounds.size.width;
    
    leftLabel.frame = CGRectMake(0, 0,(labelW + kAutoWid(40)) , textFieldHeight);
    
    self.textField.leftView = leftLabel;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    textField.edgeInsets = UIEdgeInsetsMake(0, kAutoWid(10), 0, 0);
    
    self.textField.text = self.contenValue;
    
    // 添加分割线
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0,kAutoHei(24) - 0.5, kYFScreenWidth, 0.5)];
    topLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:topLine];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(textField.frame), kYFScreenWidth, 0.5)];
    bottomLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:bottomLine];
    
}

- (void)setPickerViewUpUI {
    
    UIPickerView *pickView = [[UIPickerView alloc] init];
    self.pickerView = pickView;
    pickView.dataSource = self;
    pickView.delegate = self;
    
    self.textField.inputView = pickView;
    self.pickerView.backgroundColor = [UIColor whiteColor];
}


#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return self.pickerDataSource.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return self.pickerDataSource[row];

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    self.textField.text = self.pickerDataSource[row];
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return NO;
}

@end
